package uploader

import (
	"regexp"
	"shop-common/library/variables"
	"strconv"
	"strings"
)

type UploadS struct{}

var (
	qiniu = NewQiniuUploader()
	local = NewLocalUploader()
)

func GetDefaultStorage() IUploader {
	if IsQiniu() {
		return qiniu
	} else {
		return local
	}
}

func GetManualStorage(_type int) IUploader {
	if _type == QINIU_TYPE_ATTACHMENT {
		return qiniu
	} else {
		return local
	}
}

func IsQiniu() bool {
	return variables.ZoneSetting.Type == QINIU_TYPE_ATTACHMENT
}

func ReplaceFilename(filename string) string {
	return strings.Replace(strings.Replace(filename, "\\", "", -1), "/", "", -1)
}

func GetHostStatic(locationType int) string {
	switch locationType {
	case LOCAL_TYPE_ATTACHMENT:
		return variables.ZoneSetting.Local.Host
	case QINIU_TYPE_ATTACHMENT:
		return variables.ZoneSetting.Qiniu.Host
	}
	return variables.ZoneSetting.Qiniu.Host
}

//http://r5l4vmu5z.hn-bkt.clouddn.com/7db02b9e-7715-461b-b1ac-2b292c68b631-9bepa.png
//http://localhost:9001/static/

//获取内容的attIds
func RegexpFindAllAttIds(content string) (attIds []uint) {
	const contentRe = `<img.+?alt='|"__shop__Bucket__fileId__([0-9]+).*?>`
	subMatch := regexp.MustCompile(contentRe).FindAllSubmatch([]byte(content), -1)
	if content == "" || len(subMatch) == 0 {
		return attIds
	}
	for _, value := range subMatch {
		fileId, err := strconv.Atoi(string(value[1]))
		if err == nil {
			attIds = append(attIds, uint(fileId))
		}
	}
	return attIds
}
